﻿using System.Collections.Generic;

namespace ChessGameEngine
{
    public class Pawn : Piece
    {        
        private int jStart;        

        public Pawn(ChessEngine chessEngine, object uiElement, PieceColor color, TilePosition startPosition)
            : base(chessEngine, uiElement, color)
        {
            this.jStart = startPosition.J;
        }

        public override IEnumerable<TilePosition> GetAllPossibleMoves()
        {
            int direction = this.jStart == 1 ? 1 : -1;

            int newJ = this.Position.J + (1*direction);
            if (newJ >= 0 || newJ < ChessEngine.MaxTiles)
            {
                yield return new TilePosition { I = this.Position.I, J = newJ };

                if (this.Position.J == this.jStart && !Moved)
                    yield return new TilePosition { I = this.Position.I, J = this.Position.J + (2*direction) };

                var i = this.Position.I + 1;
                var possibleMove = new TilePosition { I = i, J = this.Position.J + (1*direction) };
                if (i < ChessEngine.MaxTiles && chessEngine.IsOpponent(possibleMove, this.Color))
                    yield return possibleMove;

                i = this.Position.I - 1;
                possibleMove = new TilePosition { I = this.Position.I - 1, J = this.Position.J + (1*direction) };
                if (i > 0 && chessEngine.IsOpponent(possibleMove, this.Color))
                    yield return possibleMove;
            }
        }

    }
}
